package cn.edu.scau.cmi.zeyang.client;

import java.util.HashSet;

import cn.edu.scau.cmi.zeyang.multiton.Multiton;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class MultitonClient extends Application{

	private int times = 1;
	private Text text = new Text("暂未输入名字");
	private TextField textField = new TextField("暂未输入名字");
	private String ans = "";

	private HashSet<Multiton> multions = Multiton.getInstance();

	@Override
	public void start(Stage primaryStage) {
		try {
			BorderPane root = new BorderPane();

			Button addButton = new Button("生成对象并输出");
			addButton.setOnAction(e->{
				Multiton.addObject(textField.getText());
				ans = "输出次数"+times++ + "\n";
				int account = 1;
				for (Multiton multiton : multions) {
					ans = ans + account++ +":"+multiton.getName() + "\n";
				}
				text.setText(ans);
			});

			root.setCenter(text);
			root.setBottom(textField);
			root.setTop(addButton);

			Scene scene = new Scene(root,400,400);
			primaryStage.setTitle("实验二：多例模型");
			primaryStage.setScene(scene);
			primaryStage.show();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		launch(args);
	}
}

